/*
 * @Description: 本地存储
 * @Author: gumingchen
 * @Email: 1240235512@qq.com
 * @Date: 2020-12-28 16:25:18
 * @LastEditors: gumingchen
 * @LastEditTime: 2021-04-30 14:01:54
 */
import cookie from 'js-cookie'
import { AUTH_KEY, AUTH_STORAGE } from '@constants'
import { StorageType } from '@enums'

/**
 * @description: 本地存储、获取、清除
 * @param {String} key 存储键值
 * @param {String} value 存储值
 * @param {String} storage 存储位置
 * @return {*}
 * @author: gumingchen
 */
function set(key, value = '', storage) {
  console.log('set called from:', new Error().stack);
  switch (storage) {
    case StorageType.COOKIE:
      cookie.set(key, value)
      break
    case StorageType.SESSION:
      sessionStorage.setItem(key, value)
      break
    case StorageType.LOCAL:
      localStorage.setItem(key, value)
      break
    default:
      cookie.set(key, value)
      break
  }
}
function get(key, storage) {
  let result
  switch (storage) {
    case StorageType.COOKIE:
      result = cookie.get(key)
      break
    case StorageType.SESSION:
      result = sessionStorage.getItem(key)
      break
    case StorageType.LOCAL:
      result = localStorage.getItem(key)
      break
    default:
      result = cookie.get(key)
      break
  }
  return result
}
function clear(key, storage) {
  console.log(`[Storage Debug] Clearing ${key} from`, storage);

  switch (storage) {
    case StorageType.COOKIE:
      cookie.remove(key)
      break
    case StorageType.SESSION:
      sessionStorage.removeItem(key)
      break
    case StorageType.LOCAL:
      localStorage.removeItem(key)
      break
    default:
      cookie.remove(key)
      break
  }
}

/**
 * @description: token-存储、获取、清除
 * @param {*}
 * @return {*}
 * @author: gumingchen
 */

function setAuth(authData) {
  if (authData.token) {
    // 1. token永远存cookie，用于请求认证
    set('token', authData.token, StorageType.COOKIE);
    
    if (authData.user) {
      // 2. 用户ID也存cookie，配合token使用
      set('userId', authData.user.userid, StorageType.COOKIE);
      
      // 3. 完整认证信息存session，用于临时会话
      set(AUTH_KEY, JSON.stringify({
        token: authData.token,
        expiredAt: authData.expiredAt
      }), StorageType.SESSION);
      set('userInfo', JSON.stringify(authData.user), StorageType.SESSION);
      // 4. 用户基本信息存local，用于持久化展示
      const{userid, email, nickname, avatar, sex, online, last_at, createdAt } = authData.user;
      const userInfo = {userid, email, nickname, avatar, sex, online, last_at, createdAt };
      set('userInfo', JSON.stringify(userInfo), StorageType.LOCAL);
    }
  }
}

function getAuth() {
  // 1. 从Cookie获取token和userId - 用于请求验证
  const token = get('token', StorageType.COOKIE) || '';
  const userId = get('userId', StorageType.COOKIE) || '';
  
  // 2. 从SessionStorage获取完整认证信息 - 临时会话数据
  const sessionAuth = JSON.parse(get(AUTH_KEY, StorageType.SESSION) || '{}');
  
  // 3. 从LocalStorage获取用户基本信息 - 长期稳定数据
  const userInfo = JSON.parse(get('userInfo', StorageType.LOCAL) || '{}');

  return {
    token,
    userId,
    ...sessionAuth,
    userInfo
  };
}

function clearAuth() {
  // 清除cookie中的token和userId
  clear('token', StorageType.COOKIE);
  clear('userId', StorageType.COOKIE);

  // 清除sessionStorage中的认证信息
  clear(AUTH_KEY, StorageType.SESSION);
  clear('userInfo', StorageType.SESSION);

  // 清除localStorage中的用户基本信息
  clear('userInfo', StorageType.LOCAL);
}
// 修改导出部分
export { 
  setAuth,
  getAuth,
  clearAuth,
  set,
  get,
  clear,
};